import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FileParser {
	
	

	@SuppressWarnings("rawtypes")
	public ArrayList parseFile(String role, String name, String conference) {
		ArrayList parseList = new ArrayList();

		try {

			File fXmlFile = new File(
					"C:\\Users\\Patrick Black\\Documents\\test.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName(role);

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					if (getTagValue("Name", eElement).equals(name)
							&& getTagValue("Conference", eElement).equals(
									conference)) {
						parseList.add(getTagValue("Manuscript", eElement));
						parseList.add(getTagValue("Underwear", eElement));
						parseList.add(getTagValue("Hand", eElement));
						
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return parseList;
	}

	private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();

		Node nValue = (Node) nlList.item(0);

		return nValue.getNodeValue();
	}
	
	
	public void editFile(Author a, String change) {
		
		
		try {
			
			String filePath = "C:\\Users\\Patrick Black\\Documents\\test.xml";
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
				.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(filePath);
			
			NodeList nList = doc.getElementsByTagName("Author");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;

					if (getTagValue("Name", eElement).equals(a.getName())
							&& getTagValue("Conference", eElement).equals(
									a.getConference())) {
						
						
						
					}
				}
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	

}